﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebNext.Domain.Entities;
using System.Web.Mvc;
using System.Xml.Serialization;
using WebNext.Domain;
using WebNext.UI.Properties;
using WebNext.UI.Utils;

namespace WebNext.UI.Models.Entries
{
	public class EntryChapterElementViewModel : EntryElementViewModel
	{
		public override EntryElementType Type
		{
			get { return EntryElementType.Chapter; }
		}

		public string Chapter
		{
			get;
			private set;
		}

		public bool IsChapterValid
		{
			get;
			private set;
		}

		public EntryChapterElementViewModel(EntryChapterElement entryChapterElement) : base(entryChapterElement.Order)
		{
			this.Chapter = entryChapterElement.Chapter;
			this.IsChapterValid = true;
		}

		public EntryChapterElementViewModel(int order, IDictionary<string, string> properties)	: base(order)
		{
			if (properties.ContainsKey("chapter"))
			{
				this.Chapter = properties["chapter"];
			}
			this.IsChapterValid = true;
		}

		public override EntryElement GetEntryElement()
		{
			return new EntryChapterElement { Chapter = this.Chapter, Order = this.Order, Type = this.Type };
		}

		public override ValidationResult Validate()
		{
			ValidationResult result = new ValidationResult();
			if (string.IsNullOrWhiteSpace(this.Chapter))
			{
				result.AddError(Resources.EntryChapterCannotBeEmpty);
				this.IsChapterValid = false;
			}
			else if (this.Chapter.Length > EntryChapterElement.MaxChapterLength)
			{
				result.AddError(Resources.EntryChapterTooLong, EntryChapterElement.MaxChapterLength);
				this.IsChapterValid = false;
			}
			return result;
		}
	}
}